using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ASPNETVN.PORTAL.Components.Configuration;
using ASPNETVN.PORTAL.Components.Configuration.Items;
using ASPNETVN.PORTAL.Components.Security;
using ASPNETVN.PORTAL.Components.UI;

namespace ASPNETVN.PORTAL.Modules.TAB
{
    public partial class Tabs : PortalModuleControl
    {
        protected override void OnInit(EventArgs e)
        {
            if (!PortalSecurity.IsInRole("1"))
            {
                addBtn.Enabled = this.IsAddable;
                deleteBtn.Enabled = this.IsDeleteable;
                upBtn.Enabled = downBtn.Enabled = editBtn.Enabled = this.IsEditable;
            }

            base.OnInit(e);
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.TabBind();
            }
        }

        private void TabBind()
        {
            Components.Configuration.Items.Portal portal = this.Portal;
            tabList.DataSource = portal.Tabs;
            tabList.DataBind();
        }

        protected void UpDown_Click(Object sender, ImageClickEventArgs e)
        {
            string cmd = ((ImageButton) sender).CommandName;            
            if (tabList.SelectedIndex >= 0)
            {
                int index = tabList.SelectedIndex;
                if (cmd == "down")
                {
                    if (index < tabList.Items.Count - 1)
                    {
                        Tab t1 = (Tab)this.Portal.Tabs[index];
                        Tab t2 = (Tab)this.Portal.Tabs[index + 1];
                        swapTabOrder(t1, t2);
                    }
                }
                else
                {
                    if (index > 0)
                    {
                        Tab t1 = (Tab)this.Portal.Tabs[index];
                        Tab t2 = (Tab)this.Portal.Tabs[index - 1];
                        swapTabOrder(t1, t2);
                    }
                }
                // Redirect to this site to refresh
                Response.Redirect("~/Default.aspx?tabindex=" + this.TabIndex + "&tabid=" + this.TabID);
            }
        }

        private static void swapTabOrder(Tab t1, Tab t2)
        {
            PortalConfiguration configuration = new PortalConfiguration();
            configuration.SwapTabOrder(t1, t2);
        }

        //*******************************************************
        //
        // The DeleteBtn_Click server event handler is used to delete
        // the selected tab from the portal
        //
        //*******************************************************

        protected void DeleteBtn_Click(Object sender, ImageClickEventArgs e)
        {
            if (tabList.SelectedIndex != -1)
            {
                // must delete from database too
                Tab t = (Tab) this.Portal.Tabs[tabList.SelectedIndex];
                PortalConfiguration config = new PortalConfiguration();
                config.DeleteTab(t.ID);

                // remove item from list
                this.Portal.Tabs.RemoveAt(tabList.SelectedIndex);

                // reorder list
                OrderTabs();

                // Redirect to this site to refresh
                Response.Redirect("~/Default.aspx?tabindex=" + this.TabIndex + "&tabid=" + this.TabID);
            }
        }


        //*******************************************************
        //
        // The AddTab_Click server event handler is used to add
        // a new security tab for this portal
        //
        //*******************************************************

        protected void AddTab_Click(Object Sender, EventArgs e)
        {
            // Obtain Portal from Current Context
            Components.Configuration.Items.Portal portal = (Components.Configuration.Items.Portal) Context.Items["Portal"];

            // New tabs go to the end of the list
            Tab t = new Tab();
            t.Name = "New Tab";
            t.ID = -1;
            t.Order = 9999;
            this.Portal.Tabs.Add(t);

            // write tab to database
            PortalConfiguration config = new PortalConfiguration();
            t.ID = config.AddTab(portal.ID, t.Name, t.Order);

            // reload the _portal from the database
            HttpContext.Current.Items["Portal"] = new Components.Configuration.Items.Portal(portal.ID, t.ID);

            // Reset the order numbers for the tabs within the list  
            OrderTabs();

            // Redirect to edit page
            string url = string.Format("~/AddEditPage.aspx?tabindex={0}&tabid={1}&mid={2}&page=AddEditTab&returnUrl={3}", this.TabIndex, t.ID, this.ModuleID, Server.UrlEncode(Request.RawUrl));
            Response.Redirect(url);
        }

        //*******************************************************
        //
        // The EditBtn_Click server event handler is used to edit
        // the selected tab within the portal
        //
        //*******************************************************

        protected void EditBtn_Click(Object sender, ImageClickEventArgs e)
        {
            // Redirect to edit page of currently selected tab
            if (tabList.SelectedIndex != -1)
            {
                // Redirect to module settings page
                Tab tab = (Tab)this.Portal.Tabs[tabList.SelectedIndex];
                string url = string.Format("~/AddEditPage.aspx?tabindex={0}&tabid={1}&mid={2}&page=AddEditTab&returnUrl={3}", this.TabIndex, tab.ID, this.ModuleID, Server.UrlEncode(Request.RawUrl));
                Response.Redirect(url);
            }
        }

        protected void OrderTabs()
        {
            this.Portal.Tabs.Sort();
            int i = 1;
            foreach (Tab t in this.Portal.Tabs)
            {
                t.Order = i;
                i += 2;
                PortalConfiguration config = new PortalConfiguration();
                config.UpdateTabOrder(t.ID, t.Order);
            }
        }
    }
}